// ��Date����չ���� Date ת��Ϊָ����ʽ��String
// ��(M)����(d)��Сʱ(h)����(m)����(s)������(q) ������ 1-2 ��ռλ���� 
// ��(y)������ 1-4 ��ռλ��������(S)ֻ���� 1 ��ռλ��(�� 1-3 λ������) 
// ���ӣ� 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //�·� 
        "d+": this.getDate(), //�� 
        "h+": this.getHours(), //Сʱ 
        "m+": this.getMinutes(), //�� 
        "s+": this.getSeconds(), //�� 
        "q+": Math.floor((this.getMonth() + 3) / 3), //���� 
        "S": this.getMilliseconds() //���� 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

$(function(){
    //���ᰴťlayer����
    $('.layer_confirm').on('click',function(e){
        e.preventDefault();
        var title = $(this).attr('title');
        var url = $(this).prop('href');
        layer.confirm(title, {icon: 3, title:'message', 
            btn: ['Confirm','cancel'] //��ť
        }, function(){
             $.getJSON(url).done(function(data) {
                    if (data.state === 'success') {
                        if (data.url) {
                            window.location.href = data.url;
                        }
                    } else if (data.state === 'fail') {
                        layer.alert(data.info);
                    }
                });
        });
    });

    var ajaxForm_list = $('form.J_ajaxFsorm');
      if (ajaxForm_list.length) {
        if ($.support) {
              //ie8�����£�����ֻ��һ���ɼ���input:textʱ��������ҳ�����ת�ύ
              ajaxForm_list.on('submit', function (e) {
                  //����ֻ��һ���ɼ���input:textʱ��enter�ύ��Ч
                  e.preventDefault();
              });
        }

        $('.J_ajax_submit_btn').bind('click', function (e) {
            e.preventDefault();
            var btn = $(this),
            form = btn.parents('form.J_ajaxFsorm');
            //ie����placeholder�ύ����
            if ($.support) {
                form.find('[placeholder]').each(function () {
                    var input = $(this);
                    if (input.val() == input.attr('placeholder')) {
                        input.val('');
                    }
                });
            }


            form.ajaxSubmit({
               url: btn.data('action') ? btn.data('action') : form.attr('action'),
                //��ť���Ƿ��Զ����ύ��ַ(�ఴť���)
                dataType: 'json',
                beforeSubmit: function (arr, $form, options) {
                    var text = btn.attr('value');

                    //��ť�İ���״̬�޸�
                    btn.attr('value',text + '...').attr('disabled', true).addClass('disabled');
                },
                success: function (data, statusText, xhr, $form) {
                    var text =  btn.attr('value');
                    //��ť�İ���״̬�޸�
                    //btn.removeClass('disabled').attr('value','text.replace('��...', '')');
                    if (data.state === 'success') {
                       btn.attr('value',text.replace('...', '') + 'success');
                        layer.msg('submit success', {
                          icon:1,
                          time: 2000 //2��رգ���������ã�Ĭ����3�룩
                        },function(){

                          if (data.url) {
                                    window.parent.location.href = data.url;
                                }
                        });
                    } else if (data.state === 'fail') {
                        layer.msg(data.info,{shift: 6});
                        btn.removeProp('disabled').removeClass('disabled').attr('value',text.replace('...', '') );
                        return false;
                    }
                }
            });
        });
      }
});

/*
 * formatMoney(s,type)
 * 功能：金额按千位逗号分割
 * 参数：s，需要格式化的金额数值.
 * 参数：type,判断格式化后的金额是否需要小数位.
 * 返回：返回格式化后的数值字符串.
 */
function formatMoney(s, type) {
    if (/[^0-9\.]/.test(s))
        return "0";
    if (s == null || s == "")
        return "0";
    s = s.toString().replace(/^(\d*)$/, "$1.");
    s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
    s = s.replace(".", ",");
    var re = /(\d)(\d{3},)/;
    while (re.test(s))
        s = s.replace(re, "$1,$2");
    s = s.replace(/,(\d\d)$/, ".$1");
    if (type == 0) {// 不带小数位(默认是有小数位)
        var a = s.split(".");
        if (a[1] == "00") {
            s = a[0];
        }
    }
    return s;
}